package com.kingwang.training.designpattern.observer.bo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashSet;
import java.util.Set;

/**
 * 任务
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Task {
    private Set<TaskWatcher> watchers = new HashSet<>();

    private String title;

    private String content;

    public void setTitle(String title) {
        this.title = title;
        noticeWatchers();
    }

    public void setContent(String content) {
        this.content = content;
        noticeWatchers();
    }

    private void noticeWatchers() {
        for (TaskWatcher watcher : this.watchers) {
            watcher.taskChanged(this);
        }
    }

    public void addWatcher(TaskWatcher watcher) {
        this.watchers.add(watcher);
    }
}
